home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBEXP.C < prev    next >
Text File  |  1991-09-23  |  11KB  |  563 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbexp.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <ctype.h>
  10. #include <errno.h>
  11. #include <limits.h>
  12. #ifdef AC_STDDEF
  13. #include <stddef.h>
  14. #endif
  15. #include <stdio.h>
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19.  
  20. /* local headers */
  21. #include "cbase_.h"
  22.  
  23. /*man---------------------------------------------------------------------------
  24. NAME
  25.      cbexp - cbase export functions
  26.  
  27. SYNOPSIS
  28.  
  29. DESCRIPTION
  30.  
  31. SEE ALSO
  32.      cbcmp, cbimp.
  33.  
  34. DIAGNOSTICS
  35.  
  36. WARNINGS
  37.      Pointer format is implementation defined; it could possibly
  38.      contain the field delimiter ('|') or escape ('\\\\') chars.  If
  39.      so, the field delimiter or escape character will have to be
  40.      changed.
  41.  
  42. ------------------------------------------------------------------------------*/
  43. /* array data type export macro */
  44. #define vexp(EXPFCT) {                            \
  45.     int i = 0;                            \
  46.     int elems = n / sizeof(*cp);                    \
  47.                                     \
  48.     for (i = 0; i < elems; ++i) {                    \
  49.         if (EXPFCT(fp, cp, sizeof(*cp)) == -1) return -1;    \
  50.         if (i < elems - 1) {                    \
  51.             if (fputc(EXPFLDDLM, fp) == EOF) return -1;    \
  52.         }                            \
  53.         ++cp;                            \
  54.     }                                \
  55.     return 0;                            \
  56. }
  57.  
  58. /* t_char -> use t_uchar export function */
  59. #define charexp        ucharexp
  60.  
  61. /* t_charv -> use t_ucharv export function */
  62. #define charvexp    ucharvexp
  63.  
  64. /* ucharexp:  t_uchar export function */
  65. #ifdef AC_PROTO
  66. static int ucharexp(FILE *fp, const void *p, size_t n)
  67. #else
  68. static int ucharexp(fp, p, n)
  69. FILE *fp;
  70. const void *p;
  71. size_t n;
  72. #endif
  73. {
  74.     unsigned char c = *(unsigned char *)p;
  75.  
  76.     switch (c) {
  77.     case EXPFLDDLM:    /* export file field delimiter */
  78.         if (fputc(EXPESC, fp) == EOF) return -1;
  79.         if (fputc('F', fp) == EOF) return -1;
  80.         break;    /* case EXPFLDDLM: */
  81.     case EXPESC:    /* export file field escape character */
  82.         if (fputc(EXPESC, fp) == EOF) return -1;
  83.         if (fputc(EXPESC, fp) == EOF) return -1;
  84.         break;    /* case EXPESC: */
  85. #ifdef AC_ESCAPE
  86.     case '\a':    /* audible alert */
  87.         if (fputc(EXPESC, fp) == EOF) return -1;
  88.         if (fputc('a', fp) == EOF) return -1;
  89.         break;    /* case '\a': */
  90. #endif
  91.     case '\b':    /* backspace */
  92.         if (fputc(EXPESC, fp) == EOF) return -1;
  93.         if (fputc('b', fp) == EOF) return -1;
  94.         break;    /* case '\b': */
  95.     case '\f':    /* form feed */
  96.         if (fputc(EXPESC, fp) == EOF) return -1;
  97.         if (fputc('f', fp) == EOF) return -1;
  98.         break;    /* case '\f': */
  99.     case '\n':    /* newline */
  100.         if (fputc(EXPESC, fp) == EOF) return -1;
  101.         if (fputc('n', fp) == EOF) return -1;
  102.         break;    /* case '\n': */
  103.     case '\r':    /* carriage return */
  104.         if (fputc(EXPESC, fp) == EOF) return -1;
  105.         if (fputc('r', fp) == EOF) return -1;
  106.         break;    /* case '\r': */
  107.     case '\t':    /* horizontal tab */
  108.         if (fputc(EXPESC, fp) == EOF) return -1;
  109.         if (fputc('t', fp) == EOF) return -1;
  110.         break;    /* case '\t': */
  111.     case '\v':    /* vertical tab */
  112.         if (fputc(EXPESC, fp) == EOF) return -1;
  113.         if (fputc('v', fp) == EOF) return -1;
  114.         break;    /* case '\v': */
  115.     default:
  116.         if (isprint(c)) {
  117.             if (fputc(c, fp) == EOF) return -1;
  118.         } else {
  119.             if (fprintf(fp, "%c%.3o", (int)EXPESC, (int)c) < 0) {
  120.                 return -1;
  121.             }
  122.         }
  123.     }
  124.  
  125.     return 0;
  126. }
  127.  
  128. /* ucharvexp:  t_ucharv export function */
  129. #ifdef AC_PROTO
  130. static int ucharvexp(FILE *fp, const void *p, size_t n)
  131. #else
  132. static int ucharvexp(fp, p, n)
  133. FILE *fp;
  134. const void *p;
  135. size_t n;
  136. #endif
  137. {
  138.     unsigned char *cp = (unsigned char *)p;
  139.  
  140.     vexp(ucharexp);
  141. }
  142.  
  143. /* shortexp:  t_short export function */
  144. #ifdef AC_PROTO
  145. static int shortexp(FILE *fp, const void *p, size_t n)
  146. #else
  147. static int shortexp(fp, p, n)
  148. FILE *fp;
  149. const void *p;
  150. size_t n;
  151. #endif
  152. {
  153.     signed short x = 0;
  154.  
  155.     memcpy(&x, p, sizeof(x));
  156.     if (fprintf(fp, "%hd", (int)x) < 0) return -1;
  157.  
  158.     return 0;
  159. }
  160.  
  161. /* shortvexp:  t_shortv export function */
  162. #ifdef AC_PROTO
  163. static int shortvexp(FILE *fp, const void *p, size_t n)
  164. #else
  165. static int shortvexp(fp, p, n)
  166. FILE *fp;
  167. const void *p;
  168. size_t n;
  169. #endif
  170. {
  171.     signed short *cp = (signed short *)p;
  172.  
  173.     vexp(shortexp);
  174. }
  175.  
  176. /* ushortexp:  t_ushort export function */
  177. #ifdef AC_PROTO
  178. static int ushortexp(FILE *fp, const void *p, size_t n)
  179. #else
  180. static int ushortexp(fp, p, n)
  181. FILE *fp;
  182. const void *p;
  183. size_t n;
  184. #endif
  185. {
  186.     unsigned short x = 0;
  187.  
  188.     memcpy(&x, p, sizeof(x));
  189.     if (fprintf(fp, "%hu", (unsigned int)x) < 0) return -1;
  190.  
  191.     return 0;
  192. }
  193.  
  194. /* ushortvexp:  t_ushortv export function */
  195. #ifdef AC_PROTO
  196. static int ushortvexp(FILE *fp, const void *p, size_t n)
  197. #else
  198. static int ushortvexp(fp, p, n)
  199. FILE *fp;
  200. const void *p;
  201. size_t n;
  202. #endif
  203. {
  204.     unsigned short *cp = (unsigned short *)p;
  205.  
  206.     vexp(ushortexp);
  207. }
  208.  
  209. /* intexp:  t_int export function */
  210. #ifdef AC_PROTO
  211. static int intexp(FILE *fp, const void *p, size_t n)
  212. #else
  213. static int intexp(fp, p, n)
  214. FILE *fp;
  215. const void *p;
  216. size_t n;
  217. #endif
  218. {
  219.     signed int x = 0;
  220.  
  221.     memcpy(&x, p, sizeof(x));
  222.     if (fprintf(fp, "%d", x) < 0) return -1;
  223.  
  224.     return 0;
  225. }
  226.  
  227. /* intvexp:  t_intv export function */
  228. #ifdef AC_PROTO
  229. static int intvexp(FILE *fp, const void *p, size_t n)
  230. #else
  231. static int intvexp(fp, p, n)
  232. FILE *fp;
  233. const void *p;
  234. size_t n;
  235. #endif
  236. {
  237.     signed int *cp = (signed int *)p;
  238.  
  239.     vexp(intexp);
  240. }
  241.  
  242. /* uintexp:  t_uint export function */
  243. #ifdef AC_PROTO
  244. static int uintexp(FILE *fp, const void *p, size_t n)
  245. #else
  246. static int uintexp(fp, p, n)
  247. FILE *fp;
  248. const void *p;
  249. size_t n;
  250. #endif
  251. {
  252.     unsigned int x = 0;
  253.  
  254.     memcpy(&x, p, sizeof(x));
  255.     if (fprintf(fp, "%u", x) < 0) return -1;
  256.  
  257.     return 0;
  258. }
  259.  
  260. /* uintvexp:  t_uintv export function */
  261. #ifdef AC_PROTO
  262. static int uintvexp(FILE *fp, const void *p, size_t n)
  263. #else
  264. static int uintvexp(fp, p, n)
  265. FILE *fp;
  266. const void *p;
  267. size_t n;
  268. #endif
  269. {
  270.     unsigned int *cp = (unsigned int *)p;
  271.  
  272.     vexp(uintexp);
  273. }
  274.  
  275. /* longexp:  t_long export function */
  276. #ifdef AC_PROTO
  277. static int longexp(FILE *fp, const void *p, size_t n)
  278. #else
  279. static int longexp(fp, p, n)
  280. FILE *fp;
  281. const void *p;
  282. size_t n;
  283. #endif
  284. {
  285.     signed long x = 0;
  286.  
  287.     memcpy(&x, p, sizeof(x));
  288.     if (fprintf(fp, "%ld", x) < 0) return -1;
  289.  
  290.     return 0;
  291. }
  292.  
  293. /* longvexp:  t_longv export function */
  294. #ifdef AC_PROTO
  295. static int longvexp(FILE *fp, const void *p, size_t n)
  296. #else
  297. static int longvexp(fp, p, n)
  298. FILE *fp;
  299. const void *p;
  300. size_t n;
  301. #endif
  302. {
  303.     signed long *cp = (signed long *)p;
  304.  
  305.     vexp(longexp);
  306. }
  307.  
  308. /* ulongexp:  t_ulong export function */
  309. #ifdef AC_PROTO
  310. static int ulongexp(FILE *fp, const void *p, size_t n)
  311. #else
  312. static int ulongexp(fp, p, n)
  313. FILE *fp;
  314. const void *p;
  315. size_t n;
  316. #endif
  317. {
  318.     unsigned long x = 0;
  319.  
  320.     memcpy(&x, p, sizeof(x));
  321.     if (fprintf(fp, "%lu", x) < 0) return -1;
  322.  
  323.     return 0;
  324. }
  325.  
  326. /* ulongvexp:  t_ulongv export function */
  327. #ifdef AC_PROTO
  328. static int ulongvexp(FILE *fp, const void *p, size_t n)
  329. #else
  330. static int ulongvexp(fp, p, n)
  331. FILE *fp;
  332. const void *p;
  333. size_t n;
  334. #endif
  335. {
  336.     unsigned long *cp = (unsigned long *)p;
  337.  
  338.     vexp(ulongexp);
  339. }
  340.  
  341. /* floatexp:  t_float export function */
  342. #ifdef AC_PROTO
  343. static int floatexp(FILE *fp, const void *p, size_t n)
  344. #else
  345. static int floatexp(fp, p, n)
  346. FILE *fp;
  347. const void *p;
  348. size_t n;
  349. #endif
  350. {
  351.     float x = 0;
  352.  
  353.     memcpy(&x, p, sizeof(x));
  354.     if (fprintf(fp, "%G", (double)x) < 0) return -1;
  355.  
  356.     return 0;
  357. }
  358.  
  359. /* floatvexp:  t_floatv export function */
  360. #ifdef AC_PROTO
  361. static int floatvexp(FILE *fp, const void *p, size_t n)
  362. #else
  363. static int floatvexp(fp, p, n)
  364. FILE *fp;
  365. const void *p;
  366. size_t n;
  367. #endif
  368. {
  369.     float *cp = (float *)p;
  370.  
  371.     vexp(floatexp);
  372. }
  373.  
  374. /* dblexp:  t_double export function */
  375. #ifdef AC_PROTO
  376. static int dblexp(FILE *fp, const void *p, size_t n)
  377. #else
  378. static int dblexp(fp, p, n)
  379. FILE *fp;
  380. const void *p;
  381. size_t n;
  382. #endif
  383. {
  384.     double x = 0;
  385.  
  386.     memcpy(&x, p, sizeof(x));
  387.     if (fprintf(fp, "%G", x) < 0) return -1;
  388.  
  389.     return 0;
  390. }
  391.  
  392. /* dblexp:  t_double export function */
  393. #ifdef AC_PROTO
  394. static int dblvexp(FILE *fp, const void *p, size_t n)
  395. #else
  396. static int dblvexp(fp, p, n)
  397. FILE *fp;
  398. const void *p;
  399. size_t n;
  400. #endif
  401. {
  402.     double *cp = (double *)p;
  403.  
  404.     vexp(dblexp);
  405. }
  406.  
  407. /* ldblexp:  t_ldouble export function */
  408. #ifdef AC_PROTO
  409. static int ldblexp(FILE *fp, const void *p, size_t n)
  410. #else
  411. static int ldblexp(fp, p, n)
  412. FILE *fp;
  413. const void *p;
  414. size_t n;
  415. #endif
  416. {
  417. #ifdef AC_LDOUBLE
  418.     long double x = 0;
  419.  
  420.     memcpy(&x, p, sizeof(x));
  421.     if (fprintf(fp, "%LG", x) < 0) return -1;
  422.  
  423.     return 0;
  424. #else
  425.     return -1;
  426. #endif
  427. }
  428.  
  429. /* ldblvexp:  t_ldoublev export function */
  430. #ifdef AC_PROTO
  431. static int ldblvexp(FILE *fp, const void *p, size_t n)
  432. #else
  433. static int ldblvexp(fp, p, n)
  434. FILE *fp;
  435. const void *p;
  436. size_t n;
  437. #endif
  438. {
  439. #ifdef AC_LDOUBLE
  440.     long double *cp = (long double *)p;
  441.  
  442.     vexp(ldblexp);
  443. #else
  444.     return -1;
  445. #endif
  446. }
  447.  
  448. /* ptrexp:  t_pointer export function */
  449. #ifdef AC_PROTO
  450. static int ptrexp(FILE *fp, const void *p, size_t n)
  451. #else
  452. static int ptrexp(fp, p, n)
  453. FILE *fp;
  454. const void *p;
  455. size_t n;
  456. #endif
  457. {
  458.     void *x = NULL;
  459.  
  460.     memcpy(&x, p, sizeof(x));
  461.     if (fprintf(fp, "%p", x) < 0) return -1;
  462.  
  463.     return 0;
  464. }
  465.  
  466. /* ptrvexp:  t_pointerv export function */
  467. #ifdef AC_PROTO
  468. static int ptrvexp(FILE *fp, const void *p, size_t n)
  469. #else
  470. static int ptrvexp(fp, p, n)
  471. FILE *fp;
  472. const void *p;
  473. size_t n;
  474. #endif
  475. {
  476.     void **cp = (void **)p;
  477.  
  478.     vexp(ptrexp);
  479. }
  480.  
  481. /* strexp:  t_string export function */
  482. #ifdef AC_PROTO
  483. static int strexp(FILE *fp, const void *p, size_t n)
  484. #else
  485. static int strexp(fp, p, n)
  486. FILE *fp;
  487. const void *p;
  488. size_t n;
  489. #endif
  490. {
  491.     char *s = (char *)p;
  492.     int i = 0;
  493.  
  494.     for (i = 0; i < n; ++i) {
  495.         if (s[i] == NUL) {
  496.             break;
  497.         }
  498.         if (ucharexp(fp, &s[i], sizeof(*s)) == -1) {
  499.             return -1;
  500.         }
  501.     }
  502.  
  503.     return 0;
  504. }
  505.  
  506. /* t_cistring -> use t_string export function */
  507. #define cistrexp    strexp
  508.  
  509. /* binexp:  t_binary export function */
  510. #ifdef AC_PROTO
  511. static int binexp(FILE *fp, const void *p, size_t n)
  512. #else
  513. static int binexp(fp, p, n)
  514. FILE *fp;
  515. const void *p;
  516. size_t n;
  517. #endif
  518. {
  519.     /* calculate number of hex digits for each char
  520.          # bits in char == CHAR_BIT, # bits in hex digit == 4 */
  521.     const int hexdigits = (CHAR_BIT + (4 - 1)) / 4;
  522.     unsigned char *pi = (unsigned char *)p;
  523.     char fmt[24];            /* printf format string */
  524.  
  525.     sprintf(fmt, "%%.%dX", hexdigits);
  526.     while (pi < ((unsigned char *)p + n)) {
  527.         if (fprintf(fp, fmt, (unsigned int)*pi++) < 0) return -1;
  528.     }
  529.  
  530.     return 0;
  531. }
  532.  
  533. /* export function table definition */
  534. const cbexp_t cbexpv[] = {
  535.     charexp,        /* t_char    =  0 */
  536.     charvexp,        /* t_charv    =  1 */
  537.     ucharexp,        /* t_uchar    =  2 */
  538.     ucharvexp,        /* t_ucharv    =  3 */
  539.     shortexp,        /* t_short    =  4 */
  540.     shortvexp,        /* t_shortv    =  5 */
  541.     ushortexp,        /* t_ushort    =  6 */
  542.     ushortvexp,        /* t_ushortv    =  7 */
  543.     intexp,            /* t_int    =  8 */
  544.     intvexp,        /* t_intv    =  9 */
  545.     uintexp,        /* t_uint    = 10 */
  546.     uintvexp,        /* t_uintv    = 11 */
  547.     longexp,        /* t_long    = 12 */
  548.     longvexp,        /* t_longv    = 13 */
  549.     ulongexp,        /* t_ulong    = 14 */
  550.     ulongvexp,        /* t_ulongv    = 15 */
  551.     floatexp,        /* t_float    = 16 */
  552.     floatvexp,        /* t_floatv    = 17 */
  553.     dblexp,            /* t_double    = 18 */
  554.     dblvexp,        /* t_doublev    = 19 */
  555.     ldblexp,        /* t_ldouble    = 20 */
  556.     ldblvexp,        /* t_ldoublev    = 21 */
  557.     ptrexp,            /* t_pointer    = 22 */
  558.     ptrvexp,        /* t_pointerv    = 23 */
  559.     strexp,            /* t_string    = 24 */
  560.     cistrexp,        /* t_cistring    = 25 */
  561.     binexp,            /* t_binary    = 26 */
  562. };
  563.